#include "Protocols/ShowableItem.h"
#include "protocols/Telnet.h"
#include "utilities/ReadablePacket.h"
#include "protocols/ProtocalCode.h"
#include "protocols/ProtocalPool.h"
#include "utilities/utilities.h"
#include <QtCore/QStringList>

REGISTER_PROTOCOL(Telnet)

using boost::shared_ptr;


void TelnetHeader::toShowableItems(ShowableItem* root) const{
	QString str = toHexString(mData,getHeaderLength());
	int pos = mPos;
	ShowableItem* tmp,* flag;
	QStringList Lines;
	QStringList Datas;
	ShowableItem* proto = createProtocalItem("Telnet Protocol",pos,getHeaderLength());
	root->pushChild(proto);

	Lines = str.split("0D0A");


	for(int i = 0;i < Lines.count();i++)
	{
		if (i==0&&Lines[i].data()[2]=='F'&&Lines[i].data()[3] =='F')
		{
			Datas = Lines[i].split("FF");
			for (int j=1;j < Datas.count();j++)
			{
				if (Datas[j].data()[0]=='E'&&Datas[j].data()[1]=='C')
				{
					tmp = createItem("Command:EOF","",pos,Datas[j].size()/2+1);
					proto->pushChild(tmp);
				}
				if (Datas[j].data()[0]=='E'&&Datas[j].data()[1]=='F')
				{
					tmp = createItem("Command:EOF","",pos,Datas[j].size()/2+1);
					proto->pushChild(tmp);
				}
				if (Datas[j].data()[0]=='F'&&Datas[j].data()[1]=='0')
				{
					tmp = createItem("Command:Suboption End","",pos,Datas[j].size()/2+1);
					proto->pushChild(tmp);
				}
				if (Datas[j].data()[0]=='F'&&Datas[j].data()[1]=='A')
				{
					if (Datas[j].data()[2]=='1'&&Datas[j].data()[3]=='8')
					{
						tmp = createItem("Suboption Begin: Terminal Type","",pos,Datas[j].size()/2+1);
						proto->pushChild(tmp);
					}
					if (Datas[j].data()[2]=='2'&&Datas[j].data()[3]=='5')
					{
						tmp = createItem("Suboption Begin: Authentication option","",pos,Datas[j].size()/2+1);
						proto->pushChild(tmp);
					}
				}
				if (Datas[j].data()[0]=='F'&&Datas[j].data()[1]=='B')
				{
					if (Datas[j].data()[2]=='1'&&Datas[j].data()[3]=='8')
					{
						tmp = createItem("Command:WILL Terminal Type","",pos,Datas[j].size()/2+1);
						proto->pushChild(tmp);
					}	
				}
				if (Datas[j].data()[0]=='F'&&Datas[j].data()[1]=='C')
				{
					if (Datas[j].data()[2]=='F'&&Datas[j].data()[3]=='B')
					{
						tmp = createItem("Command:WILLNT Terminal Type","",pos,Datas[j].size()/2+1);
						proto->pushChild(tmp);
					}	
				}
				if (Datas[j].data()[0]=='F'&&Datas[j].data()[1]=='D')
				{
					if (Datas[j].data()[2]=='1'&&Datas[j].data()[3]=='8')
					{
						tmp = createItem("Command:DO Terminal Type","",pos,Datas[j].size()/2+1);
						proto->pushChild(tmp);
					}	
				}
				if (Datas[j].data()[0]=='F'&&Datas[j].data()[1]=='E')
				{
					if (Datas[j].data()[2]=='1'&&Datas[j].data()[3]=='8')
					{
						tmp = createItem("Command:DONT Terminal Type","",pos,Datas[j].size()/2+1);
						proto->pushChild(tmp);
					}	
				}
			}
			pos += 2;
		}
		else if (i!= 0&&Lines[i].data()[0]=='F'&&Lines[i].data()[1] =='F'){}
// 		{
// 			Datas = Lines[i].split("FF");
// 			for (int j=1;j < Datas.count();j++)
// 			{
// 				if (Datas[j].data()[0]=='E'&&Datas[j].data()[1]=='C')
// 				{
// 					tmp = createItem("Command:EOF","",pos,Datas[j].size()/2+1);
// 					proto->pushChild(tmp);
// 				}
// 				if (Datas[j].data()[0]=='E'&&Datas[j].data()[1]=='F')
// 				{
// 					tmp = createItem("Command:EOR","",pos,Datas[j].size()/2+1);
// 					proto->pushChild(tmp);
// 				}
// 				if (Datas[j].data()[0]=='F'&&Datas[j].data()[1]=='0')
// 				{
// 					tmp = createItem("Command:Suboption End","",pos,Datas[j].size()/2+1);
// 					proto->pushChild(tmp);
// 				}
// 				if (Datas[j].data()[0]=='F'&&Datas[j].data()[1]=='A')
// 				{
// 					if (Datas[j].data()[2]=='1'&&Datas[j].data()[3]=='8')
// 					{
// 						tmp = createItem("Suboption Begin: Terminal Type","",pos,Datas[j].size()/2+1);
// 						proto->pushChild(tmp);
// 					}
// 					if (Datas[j].data()[2]=='2'&&Datas[j].data()[3]=='5')
// 					{
// 						tmp = createItem("Suboption Begin: Authentication option","",pos,Datas[j].size()/2+1);
// 						proto->pushChild(tmp);
// 					}
// 				}
// 				if (Datas[j].data()[0]=='F'&&Datas[j].data()[1]=='B')
// 				{
// 					if (Datas[j].data()[2]=='1'&&Datas[j].data()[3]=='8')
// 					{
// 						tmp = createItem("Command:WILL Terminal Type","",pos,Datas[j].size()/2+1);
// 						proto->pushChild(tmp);
// 					}	
// 				}
// 				if (Datas[j].data()[0]=='F'&&Datas[j].data()[1]=='C')
// 				{
// 					if (Datas[j].data()[2]=='2'&&Datas[j].data()[3]=='8')
// 					{
// 						tmp = createItem("Command:WILLNT Terminal Type","",pos,Datas[j].size()/2+1);
// 						proto->pushChild(tmp);
// 					}	
// 				}
// 				if (Datas[j].data()[0]=='F'&&Datas[j].data()[1]=='D')
// 				{
// 					if (Datas[j].data()[2]=='2'&&Datas[j].data()[3]=='8')
// 					{
// 						tmp = createItem("Command:DO Terminal Type","",pos,Datas[j].size()/2+1);
// 						proto->pushChild(tmp);
// 					}	
// 				}
// 				if (Datas[j].data()[0]=='F'&&Datas[j].data()[1]=='D')
// 				{
// 					if (Datas[j].data()[2]=='2'&&Datas[j].data()[3]=='8')
// 					{
// 						tmp = createItem("Command:DONT Terminal Type","",pos,Datas[j].size()/2+1);
// 						proto->pushChild(tmp);
// 					}	
// 				}
// 			}
//			pos += 2;
// 		}
		else
		{
			tmp = createItem("Data:"+Lines[i],"",pos,Lines[i].size()/2);
			proto->pushChild(tmp);
			pos += 2;
		}
	}
} 

QString TelnetHeader::getSummary() const{
	QString* str = new QString((const char *)mData);
	QStringList Lines;

	Lines = str->split("\r\n");
	return Lines[0];
}

int TelnetHeader::getHeaderLength() const{
	return mLength;
}
Telnet::Telnet():Protocol(TELNET,QString("TELNET"))
{
	shared_ptr<AppProtocolProperty> prop = make_shared<AppProtocolProperty>();
	prop->mDefaultPort.push_back(23);
	mInfo = prop;
}

boost::shared_ptr<PacketHeader> 
Telnet::analyzeProc(shared_ptr<ReadablePacket> packet,int low,int high,shared_ptr<Protocol>& tip)
{
	shared_ptr<TelnetHeader> header(new TelnetHeader(PROTOCOL_POINTER(Telnet),packet->getData(),low,high - low + 1));
	tip.reset();

	return header;
}